//
//  LoginView.m
//  SameWorld
//
//  Created by 李小朋 on 2017/3/24.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "LoginView.h"

@interface LoginView ()<UITextFieldDelegate>

@end

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    
    // Logo
    UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic__logo"]];
    CGFloat maxX = (self.frame.size.width - 60)/2;
    CGFloat maxY = self.frame.origin.y + 60;
    logoImageView.frame = CGRectMake(maxX, maxY, 60, 60);
    [self addSubview:logoImageView];
    
    // 账号图片
    UIImageView *userNameImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zhanghao"]];
    CGFloat maxNameY = (44-18)/2;
    userNameImage.frame = CGRectMake(12, 158 + maxNameY, 18, 18);
    [self addSubview:userNameImage];
    
    // 密码图片
    UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mima"]];
    CGFloat maxPasswordY = (44-18)/2;
    passwordImage.frame = CGRectMake(12, 202 + maxPasswordY, 18, 18);
    [self addSubview:passwordImage];
    
    // 下划线
    UIView *lineViewOne = [[UIView alloc] initWithFrame:(CGRectMake(34, 202, size_width - 46, 1))];
    lineViewOne.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [self addSubview:lineViewOne];
    
    UIView *lineViewTwo = [[UIView alloc] initWithFrame:(CGRectMake(34, 246, size_width - 46, 1))];
    lineViewTwo.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [self addSubview:lineViewTwo];
    
    // 账号
    UITextField *userName = [[UITextField alloc] initWithFrame:(CGRectMake(userNameImage.frame.size.width + userNameImage.frame.origin.x + 8, 158, self.frame.size.width - 46, 44))];
    userName.text = [userDef objectForKey:@"userName"];
    userName.placeholder = @"请输入账号名";
    userName.font = [UIFont fontWithName:@"HelveticaNeue"  size:16];
    userName.clearButtonMode = UITextFieldViewModeAlways;
    [self addSubview:userName];
    self.userName = userName;
    
    // 密码
    UITextField *userPassword = [[UITextField alloc] initWithFrame:(CGRectMake(passwordImage.frame.size.width + passwordImage.frame.origin.x + 8, 202, self.frame.size.width - 76, 44))];
    userPassword.text = [userDef objectForKey:@"userPassword"];
    userPassword.placeholder = @"请输入密码";
    userPassword.font = [UIFont fontWithName:@"HelveticaNeue"  size:16];
    userPassword.secureTextEntry = YES;
    userPassword.clearButtonMode = UITextFieldViewModeAlways;
    userPassword.delegate = self;
    [self addSubview:userPassword];
    self.userPassword = userPassword;
    
    // 安全模式
    UIButton *secureBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    secureBtn.frame = CGRectMake(userPassword.frame.size.width + userPassword.frame.origin.x + 8, 202+(44-18)/2, 18, 18);
    [secureBtn setImage:[UIImage imageNamed:@"bukejian"] forState:(UIControlStateNormal)];
    [secureBtn setImage:[UIImage imageNamed:@"kejian"] forState:(UIControlStateSelected)];
    [secureBtn addTarget:self action:@selector(secureBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:secureBtn];
    self.secureBtn = secureBtn;

    // 登录
    UIButton *loginBtn = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
    loginBtn.frame = CGRectMake(20 , 275, self.frame.size.width - 40, 44);
    [loginBtn setTitle:@"登录" forState:(UIControlStateNormal)];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:(UIControlStateNormal)];
    [loginBtn setBackgroundColor:[UIColor colorWithHexString:@"ff9900"]];
    loginBtn.layer.masksToBounds = YES;
    loginBtn.layer.cornerRadius = 5;
    [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginBtn];
    self.loginBtn = loginBtn;
    
    // 登录加载圈
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
    activityView.frame = CGRectMake(loginBtn.centerX , 10, 20, 20);
    [loginBtn addSubview:activityView];
    self.activityView = activityView;
    
    // 动态登录
    UIButton *dynamicLoginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    dynamicLoginBtn.frame = CGRectMake(self.frame.origin.x, loginBtn.frame.size.height + loginBtn.frame.origin.y + 22, 100, 20);
    [dynamicLoginBtn setTitleColor:[UIColor colorWithHexString:@"ff9900"] forState:(UIControlStateNormal)];
    [dynamicLoginBtn setTitle:@"动态登录" forState:(UIControlStateNormal)];
    [dynamicLoginBtn addTarget:self action:@selector(dynamicLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    dynamicLoginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:dynamicLoginBtn];
    
    // 忘记密码
    UIButton *forgetBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    forgetBtn.frame = CGRectMake(size_width - 100, loginBtn.frame.size.height + loginBtn.frame.origin.y + 22, 100, 20);
    [forgetBtn setTitle:@"忘记密码" forState:(UIControlStateNormal)];
    [forgetBtn addTarget:self action:@selector(forgetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [forgetBtn setTitleColor:color_textTwo forState:(UIControlStateNormal)];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:forgetBtn];
    
    
    // 我要入驻
    UILabel *myEnterLab = [[UILabel alloc] initWithFrame:(CGRectMake((size_width - 50)/2, size_height - 100, 100, 20))];
    myEnterLab.text = @"我要入驻";
    myEnterLab.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterAction:)];
    [myEnterLab addGestureRecognizer:tap];
    myEnterLab.font = [UIFont systemFontOfSize:14];
    myEnterLab.textColor = color_textTwo;
    [self addSubview:myEnterLab];
    

    
}
//登录按钮事件
- (void)loginAction:(UIButton *)sender {
 
    self.loginClicked();
    
}

#pragma mark -- 设置密码安全
- (void)secureBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        self.userPassword.secureTextEntry = NO;
    }else if(sender.selected == NO){
        self.userPassword.secureTextEntry = YES;
    }
    
}

#pragma mark -- 动态登录事件
- (void)dynamicLoginAction:(UIButton *)sender {
    NSLog(@"动态登录事件");
    self.dynamicLoginClicked();
}
#pragma mark -- 忘记密码
- (void)forgetPassword:(UIButton *)sender {
    NSLog(@"忘记密码");
    self.forgetPasswordClicked(sender);
}
#pragma mark -- 入驻商家按钮
- (void)enterAction:(UITapGestureRecognizer *)sender {
    NSLog(@"商家入驻");
    self.enterClicked();
}


@end
